package top.shiyiri.spring5.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import top.shiyiri.spring5.dao.UserDao;

/**
 * @author Aunean
 * @date 2022/2/18 16:10
 */
//noRollbackFor = {ArithmeticException.class},
@Transactional(readOnly = false, timeout = -1, propagation = Propagation.REQUIRED, isolation = Isolation.REPEATABLE_READ)
@Service
public class UserService {

    @Autowired
    private UserDao userDao;

    //转账的方法
    public void accountMoney() {
//        try {
            //第一步，开启事务

            //第二部，进行业务操作
            //Lucy少100
            userDao.reduceMoney();

            //模拟异常
            int i = 1/0;

            //Mary多一百
            userDao.addMoney();

            //第三步，没有发生异常，提交事务
//        } catch (Exception e) {
//            //出现异常，事务回滚
//        }
    }
}
